<template>
	<div>
		<BaseContainer
			class="report-table-wrapper"
			v-loading="loading"
			element-loading-text="加载中..."
			element-loading-background="rgba(255, 255, 255, 0.8)"
		>
			<div class="t-header">
				<div @click="drawer = !drawer" class="article-title">
					<span>政府养老扶助对象入住养老机构减免、冲抵费用申请表</span>
				</div>
				<div style="text-align: center; font-size: 15px; font-weight: 700">（所属时间：{{ editData.belongDate }} ）</div>
			</div>
			<ReportTable
				:key="tableKey"
				:column="list"
				:isDetail="true"
				:moreNewsModel="moreNewsModel"
				:baseModel="baseModel"
				:model="modelData"
				@change="handleSave"
			>
				<template v-slot:one>
					<span>{{ modelData.oneVal }}</span>
				</template>
				<template v-slot:two>
					<span>{{ modelData.twoVal }}</span>
				</template>
				<template v-slot:three>
					<span>{{ modelData.threeVal }}</span>
				</template>
				<template v-slot:four>
					<span>{{ modelData.fourVal }}</span>
				</template>
				<template v-slot:five>
					<span>{{ modelData.fiveVal }}</span>
				</template>
				<template v-slot:six>
					<span>{{ modelData.sixVal }}</span>
				</template>
				<template v-slot:note>
					<div class="w-100 tr flex-column" style="min-height: 120px; line-height: 60px">
						<div class="p-r-50">
							<span style="text-align: center">本机构承诺以上及所附数据资料真实有效，如有不实，愿承担相关法规之处罚。</span>
							<p style="padding-right: 3vw">负责人： （单位盖章）</p>
							<p style="padding-right: 6vw">年 月 日</p>
						</div>
					</div>
				</template>
				<template v-slot:note2>
					<div class="w-100 tr flex-column" style="min-height: 120px; line-height: 60px">
						<div class="p-r-50">
							<p style="padding-right: 3vw">区民政部门：（章）</p>
							<p style="padding-right: 3vw">年 月 日</p>
						</div>
					</div>
				</template>
			</ReportTable>
			<OrgArchives ref="orgArchivesEl"></OrgArchives>
			<aplid-dialog :append-to-body="true" v-model="openPersonDetail" title="名单" width="85%" @close="personDetailClose">
				<ElderArchives
				    :isPigeonhole = "isPigeonhole"
					:applyDate="applyDate"
					:monthNum="monthNum"
					:nowRowData="modelData"
					:otherData="otherData"
					:isDetail="isDetail"
					@getMonthVal="getMonthVal"
				></ElderArchives>
			</aplid-dialog>
		</BaseContainer>
	</div>
</template>

<script setup lang="ts">
import ReportTable from '../ReportTable/index.vue'
import OrgArchives from '../OrgArchives/index.vue'
import apiMod from '@/apiMod'
import { filterEmptyObj } from '@/utils/util'
import ElderArchives from '../ElderArchives/index.vue'
import { storeToRefs } from 'pinia'
import {useCommonStore} from '@/stores/common'
const { setApplyDateComp } = useCommonStore()
const { applyDateComp } = storeToRefs(useCommonStore())

const props = defineProps<{
	params?: any
	creditCode?: string | number
	editData?: any
	isDetail?: boolean
	caseId?: string | number
	custome?: boolean
	approvalStatus?: string | number
	isPigeonhole?:boolean
	otherData?:any
}>()

const emit = defineEmits(['save', 'getTableData'])

const model = ref({
	year: '',
	month: '',
})

const drawer = ref(false)
const baseModel: any = shallowRef({})
const loading = ref(false)
const modelData: any = ref({})
const orgArchivesEl = ref(null)
let moreNewsModel: any = ref({
	organizationInfoPO: {},
})
// 打开月份中申请人数的详情
let openPersonDetail = ref(false)
let applyDate = ref('')
let monthNum = ref('')
let countList = reactive([
	[
		{
			m: '',
			prop: 'one',
			mc: { rs: 1, cs: 1 },
			archives: true,
			archivesText: '名单',
			setColor: true,
			propName: '',
			showLink: (col, model) => {
				if (model.oneVal) {
						applyDate.value = model.oneVal
						getTimePersonSum(modelData.value.oneVal, 0).then(res=>{
							openPersonDetail.value = true
						})
						monthNum.value = 'one'
				} else {
					ElMessage.error('请先填写时间')
				}
			},
		},
		{
			m: '',
			prop: 'r0c1',
			mc: { rs: 1, cs: 1 },
		},
		{ m: '', prop: 'r0c2', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r0c3', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r0c4', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r0c5', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r0c6', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r0c7', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r0c8', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r0c9', mc: { rs: 1, cs: 1 } },
	],
	[
		{
			m: '11月',
			prop: 'two',
			mc: { rs: 1, cs: 1 },
			archives: true,
			archivesText: '名单',
			setColor: true,
			showLink: (col, model) => {
				if (model.twoVal) {
					getTimePersonSum(modelData.value.twoVal,1).then(res=>{
							openPersonDetail.value = true
					})
					applyDate.value = model.twoVal
					monthNum.value = 'two'
				} else {
					ElMessage.error('请先填写时间')
				}
			},
		},
		{ m: '', prop: 'r1c1', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r1c2', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r1c3', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r1c4', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r1c5', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r1c6', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r1c7', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r1c8', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r1c9', mc: { rs: 1, cs: 1 } },
	],
	[
		{
			m: '12月',
			prop: 'three',
			archives: true,
			archivesText: '名单',
			showLink: (col, model) => {
				if (model.threeVal) {
					getTimePersonSum(modelData.value.threeVal,2).then(res=>{
							openPersonDetail.value = true
					})
					applyDate.value = model.threeVal
					monthNum.value = 'three'
				} else {
					ElMessage.error('请先填写时间')
				}
			},
			mc: { rs: 1, cs: 1 },
		},
		{ m: '', prop: 'r2c1', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r2c2', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r2c3', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r2c4', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r2c5', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r2c6', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r2c7', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r2c8', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r2c9', mc: { rs: 1, cs: 1 } },
	],
	[
		{
			m: '1月',
			prop: 'four',
			archives: true,
			archivesText: '名单',
			showLink: (col, model) => {
				if (model.fourVal) {
					getTimePersonSum(modelData.value.fourVal,3).then(res=>{
							openPersonDetail.value = true
					})
					applyDate.value = model.fourVal
					monthNum.value = 'four'
				} else {
					ElMessage.error('请先填写时间')
				}
			},
			mc: { rs: 1, cs: 1 },
		},
		{ m: '', prop: 'r3c1', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r3c2', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r3c3', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r3c4', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r3c5', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r3c6', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r3c7', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r3c8', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r3c9', mc: { rs: 1, cs: 1 } },
	],
	[
		{
			m: '2月',
			prop: 'five',
			archives: true,
			archivesText: '名单',
			showLink: (col, model) => {
				if (model.fiveVal) {
					getTimePersonSum(modelData.value.fiveVal,4).then(res=>{
							openPersonDetail.value = true
					})
					applyDate.value = model.fiveVal
					monthNum.value = 'five'
				} else {
					ElMessage.error('请先填写时间')
				}
			},
			mc: { rs: 1, cs: 1 },
		},
		{ m: '', prop: 'r4c1', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r4c2', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r4c3', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r4c4', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r4c5', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r4c6', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r4c7', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r4c8', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r4c9', mc: { rs: 1, cs: 1 } },
	],
	[
		{
			m: '3月',
			prop: 'six',
			archives: true,
			archivesText: '名单',
			showLink: (col, model) => {
				if (model.sixVal) {
					getTimePersonSum(modelData.value.sixVal,5).then(res=>{
							openPersonDetail.value = true
					})
					applyDate.value = model.sixVal
					monthNum.value = 'six'
				} else {
					ElMessage.error('请先填写时间')
				}
			},
			mc: { rs: 1, cs: 1 },
		},
		{ m: '', prop: 'r5c1', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r5c2', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r5c3', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r5c4', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r5c5', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r5c6', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r5c7', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r5c8', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r5c9', mc: { rs: 1, cs: 1 } },
	],
	[
		{ m: '合计', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c1', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c2', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c3', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c4', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c5', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c6', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c7', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c8', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'r6c9', mc: { rs: 1, cs: 1 } },
	],
])
let list = reactive([
	[{ m: '基 本 情 况', align: 'center', bl: true, mc: { rs: 1, cs: 10 } }],
	[
		{
			m: '机构名称',
			mc: { rs: 1, cs: 1 },
		},
		{
			m: '',
			mc: { rs: 1, cs: 2 },
			prop: 'organizationName',
			archives: true,
			setColor: true,
			showLink: (col, model) => {
				orgArchivesEl.value?.getServiceOrgInfo(model.creditCode)
			},
		},
		{ m: '法定代表人', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'legalUserName', mc: { rs: 1, cs: 1 } },
		{ m: '地址', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'organizationAddress', mc: { rs: 1, cs: 2 } },
		{ m: '核定床位数', mc: { rs: 1, cs: 1 } },
		{ m: '', prop: 'standardBedNum', mc: { rs: 1, cs: 1 } },
	],
	[
		{
			m: '电话',
			mc: {
				rs: 1,
				cs: 3,
			},
		},
		{
			m: '',
			prop: 'legalUserPhone',
			mc: {
				rs: 1,
				cs: 2,
			},
		},
		{
			m: '机构设置证书号（备案）',
			mc: {
				rs: 1,
				cs: 3,
			},
		},
		{
			m: '',
			prop: 'creditCode',
			mc: {
				rs: 1,
				cs: 2,
			},
		},
	],
	[
		{
			m: '员 工 概 况',
			bl: true,
			align: 'center',
			mc: {
				rs: 1,
				cs: 10,
			},
		},
	],
	[
		{ m: '员工总数' },
		{ m: '', prop: 'staffTotalNumber', mc: { rs: 1, cs: 2 } },
		{ m: '持证人数' },
		{ m: '', prop: 'staffCertificateNumber', mc: { rs: 1, cs: 1 } },
		{ m: '管理人数' },
		{ m: '', prop: 'managerNumber', mc: { rs: 1, cs: 1 } },
		{ m: '持证人数' },
		{ m: '', prop: 'managerCertificateNumber', mc: { rs: 1, cs: 2 } },
	],
	[
		{
			m: '申 请 内 容',
			bl: true,
			align: 'center',
			mc: {
				rs: 1,
				cs: 10,
			},
		},
	],
	[
		{ m: '人员类别', mc: { rs: 2, cs: 1 } },
		{ m: '政府养老扶助对象居家补贴转入', mc: { rs: 1, cs: 4 } },
		{ m: '特殊失能、失智老年人增加补贴', mc: { rs: 1, cs: 4 } },
		{ m: '总金额', mc: { rs: 4, cs: 1 } },
	],
	[
		{ m: '半失能', mc: { rs: 1, cs: 2 } },
		{ m: '失能', mc: { rs: 1, cs: 2 } },
		{ m: '低保对象', mc: { rs: 1, cs: 2 } },
		{ m: '低保边缘对象', mc: { rs: 1, cs: 2 } },
	],
	[
		{ m: '标准', mc: { rs: 1, cs: 1 } },
		{ m: '36 小时/月', mc: { rs: 1, cs: 2 } },
		{ m: '48 小时/月', mc: { rs: 1, cs: 2 } },
		{ m: '800 元/月', mc: { rs: 1, cs: 2 } },
		{ m: '400 元/月', mc: { rs: 1, cs: 2 } },
	],
	[
		{ m: '月份', mc: { rs: 1, cs: 1 } },
		{ m: '人数', mc: { rs: 1, cs: 1 } },
		{ m: '金额', mc: { rs: 1, cs: 1 } },
		{ m: '人数', mc: { rs: 1, cs: 1 } },
		{ m: '金额', mc: { rs: 1, cs: 1 } },
		{ m: '人数', mc: { rs: 1, cs: 1 } },
		{ m: '金额', mc: { rs: 1, cs: 1 } },
		{ m: '人数', mc: { rs: 1, cs: 1 } },
		{ m: '金额', mc: { rs: 1, cs: 1 } },
	],
	...countList,
	[
		{
			m: '',
			prop: 'note',
			mc: {
				rs: 1,
				cs: 10,
			},
		},
	],
	[
		{
			m: '审 批 意 见',
			bl: true,
			align: 'center',
			mc: {
				rs: 1,
				cs: 10,
			},
		},
	],
	[
		{
			m: '区民政部门意见',
		},
		{
			m: '',
			prop: 'note2',
			mc: {
				rs: 1,
				cs: 9,
			},
		},
	],
])
const getMonthVal = (data) =>{
	monthNum.value = data.prop
}
const personDetailClose = () => {
	
	if (!props.isDetail) {
		if (monthNum.value == 'one') {
			getTimePersonSum(modelData.value.oneVal, 0)
		} else if (monthNum.value == 'two') {
			getTimePersonSum(modelData.value.twoVal, 1)
		} else if (monthNum.value == 'three') {
			getTimePersonSum(modelData.value.threeVal, 2)
		} else if (monthNum.value == 'four') {
			getTimePersonSum(modelData.value.fourVal, 3)
		} else if (monthNum.value == 'five') {
			getTimePersonSum(modelData.value.fiveVal, 4)
		} else if (monthNum.value == 'six') {
			getTimePersonSum(modelData.value.sixVal, 5)
		}
	}
	backData()
}
const tableColumnConfig = (data?: any) => {
	return list
}
//  判断年份月份
let tableKey = ref(0)
const toGetMonthList = () => {
	let date = props.editData.belongDate.split('-')
	let currentMonth = Number(date[1])
	let currentYear = Number(date[0])
	let list1 = [10, 11, 12, 1, 2, 3]
	let list2 = [4, 5, 6, 7, 8, 9]
	let list3 = ['oneVal', 'twoVal', 'threeVal', 'fourVal', 'fiveVal', 'sixVal']
	if (list1.includes(currentMonth)) {
		list2.forEach((el, index) => {
			if ([10, 11, 12].includes(currentMonth)) {
				modelData.value[list3[index]] = currentYear + '-' + (el < 10 ? '0' + el : el) + '-01'
			} else {
				modelData.value[list3[index]] = currentYear - 1 + '-' + (el < 10 ? '0' + el : el) + '-01'
			}
		})
	} else if (list2.includes(currentMonth)) {
		list1.forEach((el, index) => {
			if ([1, 2, 3].includes(el)) {
				modelData.value[list3[index]] = currentYear + '-' + (el < 10 ? '0' + el : el) + '-01'
			} else {
				modelData.value[list3[index]] = currentYear - 1 + '-' + (el < 10 ? '0' + el : el) + '-01'
			}
		})
	}
	// tableKey.value = Math.random()
}

watch(
	() => modelData.value.belongDate,
	(val) => {
		if (val) {
			toGetMonthList()
		}
	},
	{
		immediate: true,
		deep: true,
	}
)
// 选取时间得到人数金额
const getTimePersonSum = async (time, index) => {
	if(props.isDetail){
		return Promise.resolve('ok')
	}else{
		let params = {
		belongDate: time,
		creditCode: props.editData.creditCode,
		applyType: 2, //(1机构2老人3从业人员)
		amountApplyType: 1,
		subsidyType: 6,
		amountSubsidyType: props.editData.subsidyType,
		belongDateList: [time],
	}
	const fn =(res) => {
		modelData.value['r' + index + 'c' + 1] = res.data[time].L3
		modelData.value['r' + index + 'c' + 2] = (Number(res.data[time].L3) * (36 * 28.6)).toFixed(1)
		modelData.value['r' + index + 'c' + 3] = res.data[time].L4L5
		modelData.value['r' + index + 'c' + 4] = (res.data[time].L4L5 * (48 * 28.6)).toFixed(1)
		modelData.value['r' + index + 'c' + 5] = res.data[time].T2
		modelData.value['r' + index + 'c' + 6] = res.data[time].T2 * 800
		modelData.value['r' + index + 'c' + 7] = res.data[time].T5
		modelData.value['r' + index + 'c' + 8] = res.data[time].T5 * 400
		modelData.value['r' + index + 'c' + 9] = (
			res.data[time].L3 * 36 * 28.6 +
			res.data[time].L4L5 * (48 * 28.6) +
			res.data[time].T2 * 800 +
			res.data[time].T5 * 400
		).toFixed(1)
		// 合计
		for (let c = 1; c < 10; c++) {
			modelData.value['r' + 6 + 'c' + c] = 0
			for (let r = 0; r < 6; r++) {
				modelData.value['r' + 6 + 'c' + c] =
					c == 2 || c == 4 || c == 9
						? (
								(parseFloat(modelData.value['r' + 6 + 'c' + c] || 0) * 100 + (parseFloat(modelData.value['r' + r + 'c' + c]) || 0) * 100) /
								100
						  ).toFixed(1)
						: (parseFloat(modelData.value['r' + 6 + 'c' + c] || 0) * 100 + (parseFloat(modelData.value['r' + r + 'c' + c]) || 0) * 100) / 100
			}
		}
		modelData.value.applicationContentPO ={amass:modelData.value.r6c9} 
		modelData.value.amass =modelData.value.r6c9
	}
	try{
		let res = await apiMod.view_ofsSubsidyApply_elderly_amount_summary(params)
	if (res.code == 0) {
		fn(res)
		// backData()
		return Promise.resolve('ok')
	}
	}catch{
		let obj = {
			data:{}
		}
		obj.data[time] = {L3:0,L4L5:0,T2:0,T5:0}
		fn(obj)
		backData()
		return Promise.reject('no')
	}
	}
}
const backData = () => {
	// 将表格数据返回
	emit('getTableData', modelData.value)
}
const dataList = ref(tableColumnConfig())

watch(()=>props.editData.belongDate,(val)=>{
	if(val){
		modelData.value = props.editData
		baseModel.value = props.editData
		if(!props.isDetail&&(val!= applyDateComp.value)){
		for (let c = 1; c < 10; c++) {
			for (let r = 0; r <= 6; r++) {
				modelData.value['r' + r + 'c' + c] = undefined
			}
		}
		
		}
		setApplyDateComp(modelData.value.belongDate)
	}
},{
	immediate:true,
	deep:true
})

onMounted(() => {
	
})

const handleSave = () => {
	emit('save', modelData.value, 'elder_relief_report', props.editData)
}

onUnmounted(() => {
})
defineExpose({ backData })
</script>

<style scoped lang="scss">
.report-table-wrapper {
	width: 100%;
	margin: 0 auto;
	padding: 10px 30px;
	top: 0;

	.t-header {
		text-align: center;
		font-size: 18px;
		font-weight: bold;
		padding: 15px 0;

		.text-sm {
			font-weight: 400;
			font-size: 16px;
		}
	}
}

.article-title {
	position: relative;
	z-index: 0;
	width: 63%;
	margin: 0 auto;
}

.text-tip {
	--base-text-bg: rgb(56 176 255 / 13%);
	position: absolute;
	width: 100%;
	height: 100%;
	background: var(--base-text-bg);
	padding: 4px;
	z-index: -1;
	&:hover {
		cursor: pointer;
	}
	.tip-text-new {
		position: absolute;
		right: 0;
		top: -10px;
		color: #fff;
		border-radius: 4px;
		padding: 2px;
		width: 40px;
		background-color: #ed6d05;
	}
	.tip-text-detail {
		background-color: #179fe3;
		position: absolute;
		right: 0;
		top: -10px;
		color: #fff;
		border-radius: 4px;
		padding: 2px;
		width: 40px;
	}
}
</style>
